package com.alpha.ioc.componentScan;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Component("sedan")
//表示一个带注释的类是一个“组件”，成为Spring管理的Bean。当使用基于注解的配置和类路径扫描时，这些类被视为自动检测的候选对象。同时@Component还是一个元注解。
@Scope("prototype")
//在创建多个相同的对象时是新建，还是指向同一个
public class Car {
    @Value("${car.brand}")
    private String brand;
    @Value("${car.color}")
    private String color;
    @Autowired
    @Qualifier("family")
    private Engine engine;
    //    JSR-250 annotation
    @Resource
    private List<Wheel> wheels;
    //    @Value("${car.productionDate}")
    private Date productionDate;

    public Car() {
    }

    public Car(String brand, String color, Engine engine, Date productionDate) {
        this.brand = brand;
        this.color = color;
        this.engine = engine;
        this.productionDate = productionDate;
    }

    public Car(String brand, String color, Engine engine, List<Wheel> whells) {
        this.brand = brand;
        this.color = color;
        this.engine = engine;
        this.wheels = whells;
    }

    @Override
    public String toString() {
        return this.hashCode() + " brand: " + brand + " ;color: " + color + " ;productionDate: " + productionDate + "  ;engine: " + engine + "  ;whells: " + wheels;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public void setEngine(Engine engine) {
        this.engine = engine;
    }

    public Engine getEngine() {
        return engine;
    }

    public String getColor() {
        return color;
    }

    public void setProductionDate(Date productionDate) {
        this.productionDate = productionDate;
    }

    public Date getProductionDate() {
        return productionDate;
    }

    @PostConstruct
    public void validate() {
        System.out.println("validate car....");
    }

    @PreDestroy
    public void dispose() {
        System.out.println("car disposed!");
    }

}

